package com.foreveross.service;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.foreveross.common.Constants;
import com.foreveross.dao.HrFavorableDAO;
import com.foreveross.dao.FavorableHouseDAO;
import com.foreveross.vo.HrHouses;
import com.foreveross.vo.HrFavorable;
import com.foreveross.vo.FavorableHouse;

@Service("favorableHouseService")
@Transactional
public class FavorableHouseService extends BaseService {

	@Resource
	HrFavorableDAO favorableInfoDAO;
	
	@Resource
	FavorableHouseDAO favorableHouseDAO;
	
	/**
	 * 管理员发布润优选
	 * @param message
	 * @param houseIds
	 * @throws Exception
	 */
	public void addHrFavorable(HrFavorable hrFavorable,String houseIds) throws Exception{
		if(null != hrFavorable.getId() && !"".equals(hrFavorable.getId())){
			boundHrFavorable(hrFavorable, houseIds);
		}else{
			hrFavorable.setIsAll(Constants.IS_ALL);
			hrFavorable.setCreateTime(new Date());
			favorableInfoDAO.save(hrFavorable);
			if(null != houseIds && !"".equals(houseIds)){
				hrFavorable.setIsAll(Constants.NOT_ALL);
				favorableInfoDAO.updateObject(hrFavorable);
				for(String houseId : houseIds.split(",")){
					HrHouses house = new HrHouses(houseId);
					
					FavorableHouse favorableHouse = new FavorableHouse(house, hrFavorable);
					favorableHouseDAO.save(favorableHouse);
				}
			}
		}
	}
	
	/**
	 * 润优选绑定楼盘
	 * @param favorableId
	 * @param houseIds
	 */
	private void boundHrFavorable(HrFavorable favorable,String houseIds) throws Exception{
		String favorableId = favorable.getId();
		deleteFavorableHouseByFavorableId(favorableId);
		if(null != houseIds && !"".equals(houseIds)){
			favorable.setIsAll(Constants.NOT_ALL);
			favorableInfoDAO.updateObject(favorable);
			for(String houseId : houseIds.split(",")){
				HrHouses house = new HrHouses(houseId);
				if(getFavorableHouseListCount(favorableId, houseId)==0){
					FavorableHouse favorableHouse = new FavorableHouse(house, favorable);
					favorableHouseDAO.save(favorableHouse);
				}
			}
		}else{
			favorable.setIsAll(Constants.IS_ALL);
			favorableInfoDAO.updateObject(favorable);
		}
	}
	
	public long getFavorableHouseListCount(String newsId,String houseId)
			throws Exception{
		return favorableHouseDAO.findFavorableHouseListCount(newsId, houseId);
	}
	
	public List<FavorableHouse> getFavorableHouseList(String newsId,String houseId,
			int pageIndex,int pageSize) throws Exception{
		return favorableHouseDAO.findFavorableHouseList(newsId, houseId, pageIndex, pageSize);
	}
	
	public void deleteFavorableHouse(String ids) throws Exception{
		if(ids == null) return ;
		for(String id :ids.split(",")){
			favorableHouseDAO.deleteFavorableHouse(id);
		}
	}
	
	private void deleteFavorableHouseByFavorableId(String favorableId) throws Exception{
		favorableHouseDAO.deleteFavorableHouseByFavorableId(favorableId);
	}
}
